﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using com.bodurov.CharacterRegionsGenerator.Collections;

namespace com.bodurov.CharacterRegionsGenerator
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var openFile = new OpenFileDialog
            {
                Filter = "Files|*.*",
                FilterIndex = 1,
                Multiselect = false
            };
            var okClicked = openFile.ShowDialog();
            if (okClicked == true)
            {
                Process(openFile.File);
            }

        }

        private void Process(FileInfo file)
        {
            FileName.Text = file.Name;
            if(file.Length > (2 * 1024 * 1024))
            {
                HtmlPage.Window.Alert("Files bigger than 2 MB are not allowed");
                return;
            }
            if (new[]{".exe", ".bat", ".rar", ".zip"}.Contains(file.Extension.ToLower()))
            {
                HtmlPage.Window.Alert("Files with that extension are not allowed");
                return;
            }
            using (var stream = file.OpenRead())
            {

                using (var reader = new StreamReader(stream))
                {
                    var s = reader.ReadToEnd();
                    Process(s);
                }
                stream.Close();
            }
        }

        private void Process(string s)
        {
            var dict = new SortedDictionary<int, char>();
            foreach (var ch in s)
            {
                dict[(int)ch] = ch;
            }
            
            Range range = null;
            var ranges = new List<Range>();
            foreach (var p in dict)
            {
                range = Range.Process(range, p.Key, ranges);
            }
            var sb1 = new StringBuilder("<CharacterRegions>");
            var sb2 = new StringBuilder();
            sb1.AppendLine();
            foreach (var r in ranges)
            {
                sb1.AppendLine("    <CharacterRegion>");
                sb1.AppendLine("        <Start>&#"+r.From+";</Start>");
                sb1.AppendLine("        <End>&#" + r.To + ";</End>");
                sb1.AppendLine("    </CharacterRegion>");

                sb2.AppendLine("Range:" + r.From + "-" + r.To);
                sb2.Append('[');
                for(var i = r.From; i <= r.To; i++)
                {
                    var ch = (char) i;
                    if (i < 0 || i >= 0xFFFF) continue;
                    sb2.Append((char)i);
                }
                sb2.Append(']');
                sb2.AppendLine();
                sb2.AppendLine();
            }
            sb1.AppendLine("</CharacterRegions>");

            try
            {
                Result1.Text = sb1.ToString();
                Result2.Text = sb2.ToString();
            }
            catch(Exception e)
            {
                HtmlPage.Window.Alert("Error:" + e.Message);
            }
        }

        private void FileDrop(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;
            if (data.GetDataPresent(DataFormats.FileDrop))
            {
                var files = data.GetData(DataFormats.FileDrop) as FileInfo[];
                if (files != null && files.Length > 0)
                {
                    Process(files[0]);
                }
            }
        }

    }
}
